package com.spzx.product.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.product.service.ProductService;
import com.spzx.rabbit.constant.MqConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ProductReceiver {

    @Autowired
    ProductService productService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PRODUCT,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_UNLOCK,durable = "true"),
            key = {MqConst.ROUTING_UNLOCK}
    ))
    public void a(Channel channel , Message message, String tradeNo) throws IOException {
        System.out.println("关闭订单同时释放库存");
        productService.unlock(tradeNo);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PRODUCT,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_MINUS,durable = "true"),
            key = {MqConst.ROUTING_MINUS}
    ))
    public void b(Channel channel , Message message, String tradeNo) throws IOException {
        System.out.println("支付成功扣减库存");
        productService.minus(tradeNo);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
